package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.LikesBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import com.mongodb.client.result.DeleteResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
@Service
@Slf4j
public class ApLikesBehaviorServiceImpl implements ApLikesBehaviorService {
    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Override
    public ResponseResult like(LikesBehaviorDTO dto) {
        ApUser user = AppThreadLocalUtils.getUser();
        if (StringUtils.isEmpty(user.getId().toString())){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"未登录，请先登录");
        }
        if (StringUtils.isEmpty(dto.getArticleId().toString())){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"文章不存在");
        }
        if (dto.getOperation()!=0&&dto.getOperation()!=1){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"操作错误");
        }

//        ApUser user =new ApUser();
//        user.setId(1);
//        log.info("模拟登录用户"+user);


        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), null);
        System.out.println(apBehaviorEntry);
        Query query = Query.query(Criteria.where("articleId").is(dto.getArticleId()).and("entryId").is(apBehaviorEntry.getId()));
        ApLikesBehavior apLikesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
        log.info("查询点赞行为是否存在"+apLikesBehavior);
        if (apLikesBehavior==null){
            if (dto.getOperation()==0){ //不存在并且是点赞操作
                ApLikesBehavior apLikesBehavior2 = ApLikesBehavior.builder()
                    .entryId(apBehaviorEntry.getId())
                    .articleId(dto.getArticleId())
                    .type((short) 0)
                    .build();
                ApLikesBehavior saveResult = mongoTemplate.insert(apLikesBehavior2);
                return ResponseResult.okResult(saveResult);
            }else{  //不存在并且是取消点赞操作
                CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW,"操作不合法");
            }
        }else{
            if(dto.getOperation()==0){ //存在并且是点赞操作
                CustException.cust(AppHttpCodeEnum.USER_AGAIN_LIKE,"用户重复点赞");
            }else{//存在并且取消点赞
                DeleteResult removeResult = mongoTemplate.remove(query, ApLikesBehavior.class);
                log.info("用户取消点赞成功"+removeResult);
                return ResponseResult.okResult(removeResult);
            }
        }
        return null;
    }
}
